/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clip.model;

import L.L;
import clip.clipApp;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

/**
 *
 * @author admin
 */
public class Options implements Serializable {
    private transient clipApp app;
    //////////////////////////////////////////////////////////////////////////////
    private static final File optionsFile = new File("options.800");
    //////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////
    //VARIABLEN DIE GESPEICHERT WERDEN SOLLEN
    //rmi port
    public int MY_SERVER_PORT = 13099;
    //httpserver port
    public int MY_HTTPSERVER_PORT = 13100;
    //maximale gleichzeitige httpconnections
    public int HTTP_MAXCONNECTIONS = 5;
    //map token auf dateipfad die übertragen werden soll
    public HashMap<String, String> downloadTokens = new HashMap<String, String>();    ///////////////
    
    // holder für geholte daten, wird so mitgespeichert über fetchBuddyList()
    public BuddyList _buddyList=null;

    public Options(clipApp app) {
        this.app=app;
        this._buddyList=app.buddyList;
        new Timer("optionsSaveTimer", true).schedule(new OptionsSaveTimer(this), 90 * 1000, 2 * 60 * 1000);
        INSTANCE = loadOptions();
        if (INSTANCE != null) {
            app.buddyList=INSTANCE._buddyList;
            INSTANCE.setApp(app);
            L.d("setze app buddyList: "+app.buddyList.toString());
        } else {
            INSTANCE = this;
        }
    }
    //////////////////////////////////////////////////////////////////////////////
    //EIGENE METHODEN
    
    
    /////////////////////////////////////////////////////////////////////////
    // setter getter quark
    ////////////
    public static Options INSTANCE;

    private class OptionsSaveTimer extends TimerTask {

        private Options instance;

        public OptionsSaveTimer(Options instance) {
            this.instance = instance;
        }

        @Override
        public void run() {
            L.d("saving options...");
            instance.saveOptions();
        }
    }

    public static Options getInstance() {
        if (INSTANCE == null) {
            //L.d("instanz noch null, erstelle neue Instanz");
            //erst laden versuchen
            INSTANCE = loadOptions();
            //wenn immernoch nich dann halt neues objekt
            if (INSTANCE == null) {
                L.e("instanz noch null, erstellen über konstruktor!");
            } else {
                L.d("options erfolgreich geladen bei instanzerstellung");
            }
        } 
        return INSTANCE;
    }

    /**
     * optionsinstanz wird gespeichert.
     */
    public void saveOptions() {
        //hier alles ausführen was noch getan werden muss vorm speichern
        //buddys in den holder rein
        _buddyList=app.buddyList;
        try {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(optionsFile));
            oos.writeObject(this);
            oos.close();
            L.d("options gespeichert, buddylist: "+this._buddyList.toString());
        } catch (Exception e) {
            L.e("error error " + e.toString());
        }
    }

    /**
     * optionsinstanz wird geholt und gespeichert (statisch)
     */
    public static void saveOptionsInstance() {
        getInstance().saveOptions();
    }
    
    //kompatiblität 
    private static Options loadOptions() {
        if (optionsFile.exists()) {
            try {
                ObjectInputStream ois;
                ois = new ObjectInputStream(new FileInputStream(optionsFile));
                Options o = (Options) ois.readObject();
                L.d("options geladen, buddylist: "+o._buddyList.toString());
                ois.close();
                if (o!=null) {
                    L.d("options erfolgreich geladen: "+o.toString());
                }
                return o;
            } catch (Exception ex) {
                optionsFile.delete();
                L.e("options gefunden aber laden fehlgeschlagen");
                return null;
            }
        } else {
            L.d("options versucht zu laden, existieren nicht.");
            return null;
        }
    }

    /**
     * options speichern beim beenden.
     * @throws java.lang.Throwable
     */
    @Override
    protected void finalize() throws Throwable {
        L.d("options gespeichert durch finalize() in options");
        saveOptions();
    }

    public void setApp(clipApp app) {
        this.app = app;
    }
//    @Override
//    public String toString() {
//        StringBuffer ret=new StringBuffer();
//        Field[] fields = this.getClass().getFields();
//        for (int i = 0; i < fields.length; i++) {
//            Field f = fields[i];
//            try {
//                ret.append(f.getName() + " : " + f.get(f).toString());
//            } catch (Exception ex) {
//                Logger.getLogger(Options.class.getName()).log(Level.SEVERE, null, ex);
//            } 
//        }
//        
//        return ret.toString();
//    }
    
    
}
